<?php
/*------------------------------------------------------------------------
 * UserSetting.php 
 *
 * 用户设置
 * 	
 * Created on 2017-01-09
 *
 * Author: hwc123 <hwc123@139.com>
 * 
 * Copyright (c) 2017 All rights reserved.
 * ------------------------------------------------------------------------
 */

namespace app\index\controller;

class UserSetting extends UserBase{

	public function index(){
		$this->add_step('个人设置');
		if($this->isPost()){
			$data['realname']    = $this->_post('realname', '请填写姓名！');
			$data['sex']         = $this->_post("sex", '请选择性别！');
			$data['province_id'] = $this->_post('province_id', '请选择省份！');
			$data['city_id']     = $this->_post('city_id', '请选择城市！');
			$data['description'] = $this->_post('description');
			// 名称判断
			if(model('Config')->is_name_filter($data['realname'])){
				$this->error('名称不能包含“' . model('Config')->tips_info . '”字符');
			}
			$where['user_id'] = $this->_user_id;
			model('User')->update($data, $where, true);
			// 更新登录信息
			model('User')->get_user_info(true);
			$this->success('修改成功！');
		}
		return $this->view();
	}

	public function change_head(){
		$this->add_step('更换头像');
		if($this->isPost()){
			$file_id = $this->_post('file_id', '请上传照片');
			$x1      = $this->_post('x1', '', 0);
			$y1      = $this->_post('y1', '', 0);
			$x2      = $this->_post('x2', '请上传照片');
			$y2      = $this->_post('y2', '请上传照片');
			$width   = $this->_post('width', '请上传照片');
			$height  = $this->_post('height', '请上传照片');
			if(model('User')->user_head_crop($this->_user_id, $file_id, $x1, $y1, $x2, $y2, $width, $height)){
				$this->success('保存成功！');
			}else{
				$this->error(model('User')->tips_info);
			}
		}
		return $this->view();
	}

	public function change_mobile(){
		$this->add_step(($this->user['mobile'] ? '更换' : '绑定') . '手机号');
		if($this->isPost()){
			$mobile  = $this->_post('mobile', '手机号不能为空！');
			$smscode = $this->_post('smscode', '短信验证码不能为空！');
			if(model('Sms')->check_smscode($mobile, $smscode)){
				 $this->error('短信验证码不正确！');
			}
			if($this->user['mobile'] && $this->_is_set_password){
				$password = $this->_post('password', '密码不能为空！');
				if(!model('User')->check_password($this->_user_id, $password)){
					$this->error(model('User')->tips_info);
				}
			}

			if(model('User')->change_mobile($this->_user_id, $mobile)){
				$this->success('修改成功！', url('index'));
			}else{
				$this->error(model('User')->tips_info);
			}
		}

		return $this->view();
	}

	// 更换E-mail
	public function change_email(){
		$this->add_step('更换E-mail');
		if($this->isPost()){
			$password = $this->_post('password', '密码不能为空！');
			$email    = $this->_post('email', 'E-mail不能为空！');
			// $password = md5($password);
			if(!D('User')->check_password($this->_user_id, $password)){
				$this->error(D('User')->tips_info);
			}
			if(!D('User')->change_email($this->_user_id, $email)){
				$this->error(D('User')->tips_info);
			}
			$this->success('修改E-mail成功！', U('index'));
		}
		$this->display();
	}
	// 修改旧密码
	public function change_password(){
		$this->add_step($this->user['is_set_password'] ? '修改密码' : '密码设置');
		if($this->isPost()){
			$old_password = $this->user['is_set_password'] ? $this->_post('old_password', "旧密码不能为空！") : '';
			$new_password = $this->_post('new_password', "密码不能为空！");
			$repassword   = $this->_post('repassword', "确认密码不能为空！");

			// $old_password = md5($old_password);
			// $new_password = md5($new_password);

			// 检查两次密码一致性
			if($repassword != $new_password){
				$this->error('两次密码不一致！');
			}
			// 登陆密码对不对
			if(!model("User")->check_password($this->_user_id, $old_password) && $this->user['is_set_password']){
				$this->error("旧密码错误，请检查后再试！");
			}
			// 修改密码
			if(model("User")->change_password($this->_user_id, $old_password, $new_password)){
				$this->success("修改成功", url('index'));
			}else{
				$this->error(model("User")->tips_info);
			}
		}

		return $this->view();
	}

}